/* ************************************************************************

   Copyright:
     2008 openWorks

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Martín Alejandro Paz

************************************************************************ */

/**
* TODO.
* 
* *Ejemplo:*
*
* <pre class='javascript'>
* TODO.
*  </pre>
*/
qx.Class.define("qx.ui.form.ow.SQL",
{
	extend : qx.core.Object,

/*
*****************************************************************************
     EVENTS
*****************************************************************************
*/
	events:
	{
	/**
		* Actualliza la estructura de la consulta SQL al usar el metodo *updateStructure()*.
		* @return {Map}
	*/
    "updateStructure" : "qx.event.type.Data",
	
	/**
		* Al finalizar la importacion de datos de la consulta SQL al usar el metodo *getData()*.
		* @return {XML}
	*/
    "dataCompleted" : "qx.event.type.Data",
	
	/**
		* Al finalizar la importacion de datos de la consulta SQL al usar el metodo *getOneData()*.
		* @return {XML}
	*/
    "oneDataCompleted" : "qx.event.type.Data"
	},
	
	/**
	* @param query {String} Consulta SQL.
	*/
	construct : function (query)
	{
		this.base(arguments);
		this._query = query;
		this._filter = "";
		this.updateStructure();
	},
	
/*
*****************************************************************************
     MEMBERS
*****************************************************************************
*/
	members :
	{
		_query : null,
		_table : null,
		_structure : null,
		_filter : null,
		
		/**
		* Trae los datos de la estructura del SQL y dispara el evento *updateStructure*.
		* @return {void}
		*/
		updateStructure : function ()
		{
			this._structure = '';
			this._structure = [];
			var ajax = new qx.io.remote.Request("xml.php", "GET", "application/xml");
			if (!ajax.hasListener("completed"))
			{
				ajax.addListener("completed", function (e)
				{
					var xml = e.getContent();
					var datosXML = xml.getElementsByTagName("fields");
					var rs = "";
					var value_null = false;
					for (var i=0; i<datosXML.length; i++)
					{
						rs = datosXML[i];
						if (rs.getAttribute("not_null") == "1")
						{
							value_null = true;
						}
						else
						{
							value_null = false;
						}
						if (this._table == null)
						{
							this._table = rs.getAttribute("table");
						}
						this._structure.push({name:rs.getAttribute("name"), type:rs.getAttribute("type"), not_null:value_null});
					}
					this.fireDataEvent('updateStructure', this._structure);
				}, this);
			}
			ajax.setParameter("routine", "sql");
			ajax.setParameter("sql", this._query);
			ajax.setAsynchronous(false);
			ajax.send();
		},
		
		/**
		* Devuelve la consulta SQL.
		* @return {String}
		*/
		getSQL : function () { return this._query; },
		
		/**
		* Devuelve la estructura de la consulta SQL.<br />
		* La estructura es de la forma:<br />
		* <ul>
		* <li>*name:* Nombre del campo.</li>
		* <li>*type:* Tipo de dato.</li>
		* <li>*not_null:* 0 (cero si es null) o 1 (no null).</li>
		* </ul>
		* @return {Map}
		*/		
		getStructure : function () { return this._structure; },

		/**
		* Devuelve la primera tabla de la consulta SQL.
		* @return {String}
		*/		
		getTable : function () { return this._table; },
		
		/**
		* Trae los datos de la consulta SQL y dispara el evento *dataCompleted*.<br />
		* @param structure {Map ? null} Filtra los datos a devolver. <br />
		* *Parametros:*
		* <ul>
		* <li>*name:* Nombre del campo a traer.</li>
		* </ul>
		* @return {void}
		*/		
		getData : function (structure)
		{
			var ajax = new qx.io.remote.Request("xml.php", "GET", "application/xml");
			if (!ajax.hasListener("completed"))
			{
				ajax.addListener("completed", function (e)
				{
					this.fireDataEvent("dataCompleted", e.getContent());
				}, this);
			}
			ajax.setParameter("routine", "getData");
			ajax.setParameter("sql", this._query + " " + this._filter);
			if (structure != null)
			{
				var r = '';
				var fields = '';
				for (r in structure)
				{
					//*************** EL 'IF' ES PARCHE PARA INTERNET EXPLORER **********************
					if (String(parseInt(r)) != 'NaN')
					{
						if (fields != '')
						{
							fields += ', ' + structure[r].name;
						}
						else
						{
							fields += structure[r].name;
						}
					}
				}
				ajax.setParameter("fields", fields);
			}
			ajax.setAsynchronous(false);
			ajax.send();
		},
		
		/**
		* Trae el primer dato de la consulta SQL y dispara el evento *oneDataCompleted*.<br />
		* @param where {String} Condicion SQL.
		* @return {void}
		*/		
		getOneData : function (where)
		{
			var ajax = new qx.io.remote.Request("xml.php", "GET", "application/xml");
			if (!ajax.hasListener("completed"))
			{
				ajax.addListener("completed", function (e)
				{
					this.fireDataEvent("oneDataCompleted", e.getContent());
				}, this);
			}
			ajax.setParameter("routine", "getOneData");
			ajax.setParameter("table", this._table);
			ajax.setParameter("where", where);
			ajax.setAsynchronous(false);
			ajax.send();
		},
		setQuery : function (query)
		{
			this._query = query;
			this.updateStructure();
		},
		setFilter : function (filter)
		{
			this._filter = filter;
		}
	}
});
